Using Sendkeys in python to press {F12} results in other keys pressed?

Posted by ThantiK on Stack Overflow See other posts from Stack Overflow or by ThantiK
Published on 2010-05-13T03:30:20Z Indexed on 2010/05/13 3:34 UTC
Read the original article Hit count: 477

Filed under:
|
|
import time
from ctypes import *
import win32gui
import win32com.client as comclt

X = 119
Y = 53

def PILColorToRGB(pil_color):
    """ convert a PIL-compatible integer into an (r, g, b) tuple """
    hexstr = '%06x' % pil_color
    # reverse byte order
    r, g, b = hexstr[4:], hexstr[2:4], hexstr[:2]
    r, g, b = [int(n, 16) for n in (r, g, b)]
    return (r, g, b)

wsh = comclt.Dispatch("WScript.Shell")
w = win32gui
user = windll.LoadLibrary("c:\\windows\\system32\\user32.dll")
h = user.GetDC(0)
gdi = windll.LoadLibrary("c:\\windows\\system32\\gdi32.dll")

while True:
    FG = w.GetWindowText(w.GetForegroundWindow()) #FG = Foreground window title.
    if FG == "World of Warcraft":
        rgb = (PILColorToRGB(gdi.GetPixel(h,X,Y))) #X, Y
        time.sleep(0.333) #don't check too often.
        if (rgb[0] >= 130): #While Pixel (X, Y) is Red...
            #print "%d %d %d" % (rgb[0], rgb[1], rgb[2]) #Debug
            wsh.SendKeys("{F12}") #Send a key.
            time.sleep(0.7) #Add some extra down-time if we send the key.
    else:
        time.sleep(5)

Basically all this code does is read a pixel on the screen, and send a key (F12) if the pixel is red. But when using this code I regularly get some phantom key-code being pressed. The application I'm using this on is obviously world of warcraft, and I have checked that all keybinds are standard keybinds. However randomly it seems I get either an up arrow, or a w pressed, which moves my character forward whenever this code executes (F12 is bound to a macro, unbound from any movement. If I press f12 with a hardware event, it does not exhibit this behavior.

What in the world could be going on here?

© Stack Overflow or respective owner

Related posts about python

Related posts about sendkeys

  • .NET sendkeys to calculator

    as seen on Stack Overflow - Search for 'Stack Overflow'
    The sendkeys code below works well for Notepad but it doesn't work for Calculator. What is the problem? (It's another problem compared to what I sent here http://stackoverflow.com/questions/2604486/c-sendkeys-problem) [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr… >>> More

  • C# sendkeys to calculator

    as seen on Stack Overflow - Search for 'Stack Overflow'
    The sendkeys code below works well for Notepad but it doesn't work for Calculator. What is the problem? (It's another problem compared to what I sent here http://stackoverflow.com/questions/2604486/c-sendkeys-problem) [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr… >>> More

  • Cisco Configuration backup with Windows Script.

    as seen on Server Fault - Search for 'Server Fault'
    We have a client with a lot of Cisco Devices and we would like to automate the backups of these devices through telnet. We have both 2003 and 2008 servers and ideally use tftp to back it up. I wrote this: Set WshShell = WScript.CreateObject("WScript.Shell") Dim fso Set fso = CreateObject("Scripting… >>> More

  • C# sendkeys problem

    as seen on Stack Overflow - Search for 'Stack Overflow'
    THe code below I copied from MSDN with a bit of modification: [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName,string lpWindowName); DllImport("User32")] public static extern bool SetForegroundWindow(IntPtr hWnd); int cnt = 0; private… >>> More

  • Sendkeys problem from .NET program

    as seen on Stack Overflow - Search for 'Stack Overflow'
    THe code below I copied from MSDN with a bit of modification: [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName,string lpWindowName); DllImport("User32")] public static extern bool SetForegroundWindow(IntPtr hWnd); int cnt = 0; private… >>> More